package com.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.entity.Category;
import com.hotel.exception.BusinessException;
import com.hotel.mapper.CategoryMapper;
import com.hotel.response.ResultCode;
import com.hotel.service.AliOssService;
import com.hotel.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotel.utils.CategoryTreeBuilder;
import com.hotel.vo.CategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author Lin
 * @since 2022-05-30
 */
@Service
public class CategoryServiceImp extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Cacheable(cacheNames = "categoryList")
    @Override
    public List<Category> getCategoryList() {
        List<Category> categories = this.baseMapper.selectList(null);
        List<Category> build = CategoryTreeBuilder.build(categories);
        return build;
    }

    @Override
    public List<Object> getCategoryId(String id) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Category> categories = this.baseMapper.selectList(wrapper);
        List<Object> objects = new ArrayList<>();
        categories.forEach(item->{
            objects.add(item.getId());
        });
        return objects;
    }

    @CacheEvict(value = "categoryList",allEntries = true)
    @Override
    public int addCategory(CategoryVO categoryVO) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("name",categoryVO.getName());
        List<Category> dishes = this.baseMapper.selectList(wrapper);
        if(dishes.size()>0){
            throw new BusinessException(ResultCode.CATEGORY_ALREADY_EXIST.getCode(),
                    ResultCode.CATEGORY_ALREADY_EXIST.getMessage());
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryVO,category);
        if (categoryVO.getStatus()==null){
            category.setStatus(1);
        }
        category.setCreateUser(1l);
        category.setUpdateUser(1l);
        return this.baseMapper.insert(category);
    }

    @Resource
    private AliOssService aliOssService;

    public String upload(MultipartFile file, String pathName) {
        return this.aliOssService.upload(file,pathName);
    }

    @Override
    public Category editCategory(String id) {
        return this.baseMapper.selectById(id);
    }

    @CacheEvict(value = "categoryList",allEntries = true)
    @Override
    public int updateCategory(CategoryVO categoryVO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryVO,category);
        return this.baseMapper.updateById(category);
    }

    @CacheEvict(value = "categoryList",allEntries = true)
    @Override
    public int deleteCategory(String id) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Category> categories = this.baseMapper.selectList(wrapper);
        if (categories.size()>0){
            //THERE_ARE_DISHES_OR_CATEGORIES
            throw new BusinessException(ResultCode.THERE_ARE_DISHES_OR_CATEGORIES.getCode(),
                    ResultCode.THERE_ARE_DISHES_OR_CATEGORIES.getMessage());
        }
        return this.baseMapper.deleteById(id);
    }

    @Override
    public IPage<Category> getNavList(String name, Integer status, Integer current, Integer size) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        Page<Category> navPage = new Page<>(current,size);
        IPage<Category> navIPage = null;
        if(StringUtils.isNotEmpty(name)){
            wrapper.like("name",name);
        }
        if (status!=null){
            wrapper.eq("status",status);
        }
        wrapper.orderByAsc("sort");
        wrapper.eq("level",1);
        navIPage=page(navPage,wrapper);
        return navIPage;
    }

    @Override
    public int deleteNavById(String id) {
        return this.baseMapper.deleteById(id);
    }

    @Override
    public int addNav(CategoryVO categoryVO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryVO,category);
        category.setParentId(0l);
        category.setLevel(1);
        category.setStatus(1);
        category.setCreateUser(1l);
        category.setUpdateUser(1l);
        return this.baseMapper.insert(category);
    }

    @Override
    public Category editNav(String id) {
        Category category = this.baseMapper.selectById(id);
        return category;
    }

    @Override
    public int updateNav(CategoryVO categoryVO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryVO,category);
        int update = this.baseMapper.updateById(category);
        return update;
    }

    @Override
    public int updateStatus(String id, Integer status) {
        UpdateWrapper<Category> categoryUpdateWrapper = new UpdateWrapper<>();
        categoryUpdateWrapper.eq("id",id);
        Integer newStatus = (status==1?0:1);
        categoryUpdateWrapper.set("status",newStatus);
        int update = this.baseMapper.update(null, categoryUpdateWrapper);
        return update;
    }

}
